package com.yanchangapp.ui.fragment.mail;

import android.app.Dialog;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zxl.baselib.ui.base.BaseFragment;
import com.zxl.baselib.util.PerfectClickListener;
import com.zxl.baselib.util.ui.UIUtils;
import com.yanchangapp.R;
import com.yanchangapp.adapter.MailListAdapter;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MailType;
import com.yanchangapp.model.bean.MailItemBean;
import com.yanchangapp.ui.activity.mail.MailDetailActivity;
import com.yanchangapp.ui.activity.mail.MailHomeActivity;
import com.yanchangapp.ui.activity.mail.MailSearchActivity;
import com.yanchangapp.ui.activity.mail.TranspondActivity;
import com.yanchangapp.ui.presenter.MailListFgPresenter;
import com.yanchangapp.ui.view.MailListFgView;
import com.yanchangapp.widget.BottomDialog;
import com.yanchangapp.widget.MailListPopupWindow;
import com.zxl.baselib.widget.TipDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * 重点...邮件列表展示页面
 * <p>
 * 待完成...搜索框
 * <p>
 * 列表item页面... 已读未读颜色
 *
 * 空白页面
 *
 * 彻底删除确认弹框
 *
 * 根据某一个具体的ID 进行删除即可...
 * 通知进行删除...
 */
public class MailListFragment extends BaseFragment<MailListFgView, MailListFgPresenter> implements MailListFgView, MailListAdapter.MailListListener {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.tvCheckAll)
    TextView tvCheckAll;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvTitleName)
    TextView tvTitleName;
    @BindView(R.id.ivArrow)
    ImageView ivArrow;
    @BindView(R.id.llShowSelect)
    LinearLayout llShowSelect;
    @BindView(R.id.tvActionState)
    TextView tvActionState;
    @BindView(R.id.rvMailList)
    RecyclerView mRvMailList;
    @BindView(R.id.flToolBar)
    FrameLayout flToolBar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.itemDelete)
    LinearLayout itemDelete;
    @BindView(R.id.itemMark)
    LinearLayout itemMark;
    @BindView(R.id.itemMove)
    LinearLayout itemMove;
    @BindView(R.id.llBottom)
    LinearLayoutCompat llBottom;
    @BindView(R.id.llEmptyMail)
    LinearLayout mLlEmptyMail;
    @BindView(R.id.llErrorNetwork)
    LinearLayout mLlErrorNetwork;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.app_bar_layout)
    AppBarLayout mAppBarLayout;
    private TextView mTvSearchBox;
    private MailType mMailType;
    private boolean isEditType;
    private MailListPopupWindow mMailListPopupWindow;
    private List<MailItemBean> mTargetLists;
    private MailListAdapter mMailListAdapter;
    // 是否已经全选
    private boolean isCheckedAll;
    private int currentPage;
    private LinearLayout mMLinearSearch;
    // 选中的列表...
    private List<Integer> mSelectLists;
    private BottomDialog mBottomMarkDialog,mBottomMoveDialog;
    private Dialog mActualDeleteDialog;
    private LinearLayout mBottomInbox,mBottomTrash,mBottomSent,mBottomDraft,mBottomSpam;
    private TextView mTvCancel,mTvEnsureDel;
    // 单选全选事件还没有进行处理呢...

    @Override
    public void init() {
        mMailType = MailType.INBOX;
        mTargetLists = new ArrayList<>();
        currentPage = 1;
        isEditType = false;
        isCheckedAll = false;
        mSelectLists = new ArrayList<>();
        initRxListener();
    }

    private void initRxListener() {
        mRxManager.on(AppConstant.NOTIFY_MAIL_DELETE_SUCCESS, (Consumer<Boolean>) aBoolean -> {
            if (aBoolean){
                actionRefreshList();
            }
        });

        mRxManager.on(AppConstant.NOTIFY_MAIL_WRITE_SUCCESS, (Consumer<Boolean>) aBoolean -> {
            if (aBoolean && mMailType == MailType.SENT){
                // 刷新...
                actionRefreshList();
            }
        });

        mRxManager.on(AppConstant.NOTIFY_MAIL_SAVE_DRAFT_SUCCESS, (Consumer<Boolean>) aBoolean -> {
            if (aBoolean && mMailType == MailType.DRAFT){
                actionRefreshList();
            }
        });
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_mail_list;
    }

    @Override
    protected MailListFgPresenter createPresenter() {
        return new MailListFgPresenter((MailHomeActivity) getActivity());
    }

    @Override
    public void initView(View rootView) {
        resetState();
        setMailListTitle();
        textStateChanged();
        initAdapter();
        initMailListener();
        setPopupList();
        // 初始化进行页面刷新
        actionRefreshList();
    }

    @Override
    public void onDestroy() {
        if (mBottomMarkDialog != null){
            mBottomMarkDialog.dismiss();
        }

        if (mBottomMoveDialog != null){
            mBottomMoveDialog.dismiss();
        }
        super.onDestroy();
    }

    public void setLookState() {
        isEditType = false;
        textStateChanged();
        showOrHideCheckbox();
        // 底部选择
        setListCanRefresh();
        hideBottomChoose();
    }

    // 设置邮件列表的标题
    private void setMailListTitle() {
        tvTitleName.setText(mMailType.getMailDetail().getDescription());
    }

    private void textStateChanged() {
        setRightText();
        setTitleIsEnable();
        setLeftState();
        llBottom.setVisibility(View.GONE);
    }

    private void setSearchBoxContent() {
        if (mTvSearchBox != null) {
            mTvSearchBox.setText(mMailType.getMailDetail().getSearchTitle());
        }
    }

    // 网络状况切换
    // 初始化状态
    private void resetState(){
        mRvMailList.setVisibility(View.VISIBLE);
        mLlEmptyMail.setVisibility(View.GONE);
        mLlErrorNetwork.setVisibility(View.GONE);
    }

    // 有内容的情况
    private void hasContentState(){
        if (mRvMailList.getVisibility() == View.GONE){
            mRvMailList.setVisibility(View.VISIBLE);
            mLlEmptyMail.setVisibility(View.GONE);
            mLlErrorNetwork.setVisibility(View.GONE);
        }
    }

    // 错误情况
    private void errState(){
        if (mLlErrorNetwork.getVisibility() == View.GONE){
            mRvMailList.setVisibility(View.GONE);
            mLlEmptyMail.setVisibility(View.GONE);
            mLlErrorNetwork.setVisibility(View.VISIBLE);
        }
    }

    // 空界面
    private void emptyState(){
        if (mLlEmptyMail.getVisibility() == View.GONE){
            mRvMailList.setVisibility(View.GONE);
            mLlEmptyMail.setVisibility(View.VISIBLE);
            mLlErrorNetwork.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        // 设置搜索框是否可点击...
        mMLinearSearch = (LinearLayout) LayoutInflater.from(_mActivity).inflate(R.layout.include_search_box, null);
        mTvSearchBox = mMLinearSearch.findViewById(R.id.tvMailSearchTheme);
        setSearchBoxContent();
        setSearchBoxClickListener(mMLinearSearch);
        mRvMailList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRvMailList.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL) );
        mMailListAdapter = new MailListAdapter(R.layout.item_mail_list_content, mTargetLists,this);
        mMailListAdapter.addHeaderView(mMLinearSearch);
        mRvMailList.setAdapter(mMailListAdapter);
        // 设置点击事件
        mMailListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox checkBox =  view.findViewById(R.id.checkbox);
                if (isEditType){
                    checkBox.toggle();
                }else {
                    // 进行页面跳转...
                    // 草稿箱进入
                    int id = mTargetLists.get(position).getId();
                    int seenFlag = mTargetLists.get(position).getSeenFlag();
                    if (mMailType != MailType.DRAFT){
                        if ((seenFlag == AppConstant.MAIL_UN_READ) && (MailType.SENT != mMailType)){
                            mTargetLists.get(position).setSeenFlag(AppConstant.MAIL_HAS_READ);
                            mMailListAdapter.notifyItemChanged(position+1);
                        }
                        MailDetailActivity.show(getActivity(),id,mMailType.getMailDetail().getDescription());
                    }else {
                        MailItemBean mailItemBean = mTargetLists.get(position);
                        TranspondActivity.showFromDraft(getActivity(),mailItemBean.getId());
                    }
                }
            }
        });
    }

    private void setSearchBoxClickListener(View view) {
        view.setOnClickListener(v -> {
            if (isEditType){
                return;
            }
            MailSearchActivity.show(MailListFragment.this.getActivity(), mMailType);
        });
    }

    // 设置右边的文字选择
    private void setRightText() {
        tvActionState.setText(isEditType ? "取消" : "编辑");
    }

    // 设置邮件类型选择
    private void setTitleIsEnable() {
        if (isEditType) {
            tvTitleName.setEnabled(false);
            llShowSelect.setEnabled(false);
            ivArrow.setEnabled(false);
        } else {
            tvTitleName.setEnabled(true);
            llShowSelect.setEnabled(true);
            ivArrow.setEnabled(true);
        }
    }

    // 展示可选择的底部状态切换按钮
    private void showBottomState(){
        if (isEditType){
            if (mSelectLists.size() == 0){
                llBottom.setVisibility(View.GONE);
            }else {
                llBottom.setVisibility(View.VISIBLE);
                switch (mMailType){
                    case INBOX:
                        itemMark.setVisibility(View.VISIBLE);
                        itemMove.setVisibility(View.VISIBLE);
                        itemDelete.setVisibility(View.GONE);
                        break;
                    case DRAFT:
                        itemMark.setVisibility(View.GONE);
                        itemMove.setVisibility(View.GONE);
                        itemDelete.setVisibility(View.VISIBLE);
                        break;
                    case SENT:
                        itemMark.setVisibility(View.GONE);
                        itemMove.setVisibility(View.VISIBLE);
                        itemDelete.setVisibility(View.GONE);
                        break;
                    case TRASH:
                        itemMark.setVisibility(View.VISIBLE);
                        itemMove.setVisibility(View.VISIBLE);
                        itemDelete.setVisibility(View.VISIBLE);
                        break;
                    case SPAM:
                        itemMark.setVisibility(View.VISIBLE);
                        itemMove.setVisibility(View.VISIBLE);
                        itemDelete.setVisibility(View.VISIBLE);
                        break;
                        default:
                            break;
                }
            }
        }else {
            // 查看状态了
            llBottom.setVisibility(View.GONE);
        }
    }

    private void setLeftState(){
        if (isEditType) {
            ivToolbarNavigation.setVisibility(View.GONE);
            tvCheckAll.setVisibility(View.VISIBLE);
            setTextCheckedDes();
        } else {
            ivToolbarNavigation.setVisibility(View.VISIBLE);
            tvCheckAll.setVisibility(View.GONE);
        }
    }

    private void setTextCheckedDes(){
        if (isCheckedAll){
            tvCheckAll.setText("取消全选");
        } else {
            tvCheckAll.setText("全选");
        }
    }

    // 进行点击事件的绑定
    private void initMailListener() {
        // 邮件类型选择...
        llShowSelect.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mMailListPopupWindow.isShowing()) {
                    mMailListPopupWindow.dismiss();
                } else {
                    mMailListPopupWindow.showAsDropDown(mAppBarLayout);
                }
            }
        });

        tvActionState.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                checkAllState();
            }
        });

        ivToolbarNavigation.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                getActivity().finish();
            }
        });

        tvCheckAll.setOnClickListener(v -> {
            isCheckedAll = !isCheckedAll;
            // 设置状态的改变...
            setTextCheckedDes();
            // 设置切换...
            if (isCheckedAll){
              //
              selectAll();
              // 设置全选
              mMailListAdapter.setSelectedAll();
            }else {
              // 清除全选
              selectCleanAll();
              mMailListAdapter.setCleanAll();
            }
        });


        // 刷新加载的事件回调处理...
        // 下拉刷新
        refreshLayout.setOnRefreshListener(refreshLayout -> UIUtils.postTaskDelay(() -> justRefresh(),1000));

        // 上拉加载
        refreshLayout.setOnLoadMoreListener(refreshLayout -> UIUtils.postTaskDelay(() -> {
            currentPage ++;
            mPresenter.loadMoreMailList(mMailType.getMailDetail().getNetState(),currentPage);
        },1000));

        // 底部操作的点击事件
        itemDelete.setOnClickListener(v -> {
            // 进行彻底删除...
            showDeleteDialog();
        });

        // 移动事件...
        itemMove.setOnClickListener(v -> showBottomMoveDialog());

        // 标记事件...
        itemMark.setOnClickListener(v -> showBottomMarkDialog());

        btnNextRefresh.setOnClickListener(v -> actionRefreshList());
    }

    private void checkAllState(){
        isEditType = !isEditType;
        textStateChanged();
        showOrHideCheckbox();
        ((MailHomeActivity) getActivity()).showHiddenButton(isEditType);
        if (isEditType){
            setListCannotRefresh();
        }else {
            setListCanRefresh();
        }
        hideBottomChoose();
    }

    private void actionSuccessWithState(){
        isEditType = false;
        isCheckedAll = false;
        textStateChanged();
        mMailListAdapter.actionSuccess();
        ((MailHomeActivity) getActivity()).showHiddenButton(isEditType);
        // 进行刷新
        actionRefreshList();
    }

    // todo 移动或者删除成功了应该还原到对应的页面

    private void showBottomMarkDialog() {
        if (mBottomMarkDialog == null){
            mBottomMarkDialog = new BottomDialog(getActivity(),0,true);
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.bottom_layout_mark, null, false);
            mBottomMarkDialog.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mBottomMarkDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            view.findViewById(R.id.markToHasRead)
                    .setOnClickListener((View v) -> {
                        mBottomMarkDialog.hide();
                        setMailListHasRead();
                    });
            view.findViewById(R.id.markToNoRead)
                    .setOnClickListener(v -> {
                        mBottomMarkDialog.hide();
                        MailListFragment.this.setMailListNoRead();
                    });
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (getActivity() != null && !getActivity().isDestroyed() && !getActivity().isFinishing()) {
                mBottomMarkDialog.show();
            }
        }else {
            if (getActivity() != null && !getActivity().isFinishing()) {
                mBottomMarkDialog.show();
            }
        }
    }

    private void showBottomMoveDialog() {
        if (mBottomMoveDialog == null){
            mBottomMoveDialog = new BottomDialog(getActivity(),0,true);
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.bottom_layout_move, null, false);
            mBottomMoveDialog.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mBottomMoveDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            mBottomInbox =  view.findViewById(R.id.llInbox);
            mBottomDraft = view.findViewById(R.id.llDraft);
            mBottomSent = view.findViewById(R.id.llSent);
            mBottomTrash = view.findViewById(R.id.llTrash);
            mBottomSpam = view.findViewById(R.id.llSpam);
            mBottomInbox .setOnClickListener(v -> hideBottomSheetWithMove(MailType.INBOX.getMailDetail().getNetState()));
            mBottomDraft.setOnClickListener(v -> hideBottomSheetWithMove(MailType.DRAFT.getMailDetail().getNetState()));
            mBottomSent.setOnClickListener(v -> hideBottomSheetWithMove(MailType.SENT.getMailDetail().getNetState()));
            mBottomTrash.setOnClickListener(v -> hideBottomSheetWithMove(MailType.TRASH.getMailDetail().getNetState()));
            mBottomSpam.setOnClickListener(v -> hideBottomSheetWithMove(MailType.SPAM.getMailDetail().getNetState()));
        }

        switch (mMailType){
            case INBOX:
                mBottomInbox.setVisibility(View.GONE);
                mBottomDraft.setVisibility(View.GONE);
                mBottomSent.setVisibility(View.GONE);
                mBottomTrash.setVisibility(View.VISIBLE);
                mBottomSpam.setVisibility(View.VISIBLE);
                break;
            case SENT:
                mBottomInbox.setVisibility(View.GONE);
                mBottomDraft.setVisibility(View.GONE);
                mBottomSent.setVisibility(View.GONE);
                mBottomTrash.setVisibility(View.VISIBLE);
                mBottomSpam.setVisibility(View.GONE);
                break;
            case TRASH:
                mBottomInbox.setVisibility(View.VISIBLE);
                mBottomDraft.setVisibility(View.GONE);
                mBottomSent.setVisibility(View.VISIBLE);
                mBottomTrash.setVisibility(View.GONE);
                mBottomSpam.setVisibility(View.VISIBLE);
                break;
            case SPAM:
                mBottomInbox.setVisibility(View.VISIBLE);
                mBottomDraft.setVisibility(View.GONE);
                mBottomSent.setVisibility(View.GONE);
                mBottomTrash.setVisibility(View.VISIBLE);
                mBottomSpam.setVisibility(View.GONE);
                break;

        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (getActivity() != null && !getActivity().isDestroyed() && !getActivity().isFinishing()) {
                mBottomMoveDialog.show();
            }
        }else {
            if (getActivity() != null && !getActivity().isFinishing()) {
                mBottomMoveDialog.show();
            }
        }
    }

    private void showDeleteDialog() {
        if (mActualDeleteDialog == null){
            mActualDeleteDialog = new TipDialog.Builder(getActivity())
                    .setTitle("彻底删除")
                    .setMessage("彻底删除后,邮件将不可恢复")
                    .setEnsureText("确定删除")
                    .setEnsureClickListener(new TipDialog.EnsureClickListener() {
                        @Override
                        public void onEnsureClick(TipDialog tipDialog) {
                            mActualDeleteDialog.dismiss();
                            mPresenter.actualDelete(mSelectLists);
                        }
                    }).build();
        }
        if (!getActivity().isFinishing()){
            mActualDeleteDialog.show();
        }
    }

    private void hideBottomSheetWithMove(String mailNetType){
        mBottomMoveDialog.hide();
        mPresenter.moveListToTargetList(mailNetType,mSelectLists);
    }


    // 设置邮件列表已读
    private void setMailListHasRead(){
        mPresenter.setMailSeenState(AppConstant.MAIL_HAS_READ,mSelectLists);
    }

    // 设置邮件列表未读
    private void setMailListNoRead(){
        mPresenter.setMailSeenState(AppConstant.MAIL_UN_READ,mSelectLists);
    }

    private void hideBottomChoose(){
        if (llBottom.getVisibility() == View.VISIBLE){
            llBottom.setVisibility(View.GONE);
        }
    }

    private void justRefresh(){
        setListCanRefresh();
        actionRefreshList();
    }


    private void setListCanRefresh() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
    }

    private void setListCannotRefresh() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
    }

    private void selectAll(){
        selectCleanAll();
        for (int i=0;i<mTargetLists.size();i++){
            mSelectLists.add(mTargetLists.get(i).getId());
        }
        Log.e("邮件列表", "selectAll: "+ Arrays.toString(mSelectLists.toArray()));
    }

    private void selectCleanAll(){
        mSelectLists.clear();
    }

    private void showOrHideCheckbox() {
        mMailListAdapter.setIsNowEdit(isEditType);
        selectCleanAll();
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    /**
     * 顶部弹出层...点击事件等
     */
    private void setPopupList() {
        mMailListPopupWindow = new MailListPopupWindow(getActivity());
        mMailListPopupWindow.setImageView(ivArrow);
        mMailListPopupWindow.setOnItemSelectedListener(mailType -> {
//            refreshLayout.setEnableLoadMore(true);
            refreshLayout.closeHeaderOrFooter();
            gotoFirst();
            mMailType = mailType;
            setMailListTitle();
            setSearchBoxContent();
            refreshLayout.resetNoMoreData();
            // todo 切换时进行网络请求
            actionRefreshList();
        });
    }

    private void gotoFirst(){
            mRvMailList.scrollToPosition(0);
            LinearLayoutManager mLayoutManager =
                    (LinearLayoutManager) mRvMailList.getLayoutManager();
            mLayoutManager.scrollToPositionWithOffset(0, 0);
    }

    // 下面2个是无效的...
    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    private void actionRefreshList() {
        resetState();
        currentPage = 1;
        mPresenter.refreshMailList(mMailType.getMailDetail().getNetState());
    }


    @Override
    public void refreshMailListSuccess(List<MailItemBean> mailItemBeanList) {
        setListCanRefresh();
        refreshLayout.finishRefresh();
        if (mailItemBeanList == null || mailItemBeanList.size() == 0) {
            // 说明是空布局
            mTargetLists.clear();
            emptyState();
        } else {
            hasContentState();
            mTargetLists.clear();
            mTargetLists.addAll(mailItemBeanList);
        }
        mMailListAdapter.setIsNowEdit(false);
        selectCleanAll();
        mMailListAdapter.notifyDataSetChanged();
        gotoFirst();
    }

    @Override
    public void refreshMailListFailure(String msg) {
        UIUtils.showToast(msg);
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();
        }
        // 如果为空...
        if (mTargetLists.size() == 0){
            errState();
        }
    }

    @Override
    public void loadMoreListSuccess(List<MailItemBean> mailItemBeanList) {
        if (null == mailItemBeanList || mailItemBeanList.size() == 0) {
            // 说明是无数据了
            currentPage--;
            refreshLayout.finishLoadMoreWithNoMoreData();
        } else {
            mTargetLists.addAll(mailItemBeanList);
            mMailListAdapter.notifyDataSetChanged();
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void loadMoreListFailure(String msg) {
        UIUtils.showToast(msg);
        currentPage --;
    }

    // 一般操作成功都是要关闭对应的状态
    // 彻底删除成功
    @Override
    public void actualDeleteSuccess() {
        actionSuccessWithState();
    }

    // 彻底删除失败
    @Override
    public void actualDeleteFailure(String err) {
        UIUtils.showToast(err);
    }

    @Override
    public void setSeenStateSuccess() {
        actionSuccessWithState();
    }

    @Override
    public void setSeenStateFailure(String err) {
        UIUtils.showToast(err);
    }

    @Override
    public void moveMailSuccess() {
        actionSuccessWithState();
    }

    @Override
    public void moveMailFailure(String err) {
        UIUtils.showToast(err);
    }

    // checkbox 切换事件...
    @Override
    public void onCheckChangedListener(int position, boolean state) {
        Log.e("邮件列表", "onCheckChangedListener: "+position );
        position = position - 1;
        if (position > mTargetLists.size() - 1) {
            return;
        }
        int currentID = mTargetLists.get(position).getId();
        int index = mSelectLists.indexOf(currentID);
        if (state) {
            // 进行添加...
            if (index == -1){
                mSelectLists.add(currentID);
            }
        } else {
            if (index != -1) {
                mSelectLists.remove(index);
            }
        }
        isCheckedAll = mSelectLists.size() == mTargetLists.size();
        setTextCheckedDes();
        Log.e("邮件列表", "onCheckChangedListener: "+ Arrays.toString(mSelectLists.toArray()));
        showBottomState();
    }
}
